/* 
 * Gneat (http://gneat.org)
 *
 * (C) Copyright 2011 and beyond - Claudio Stella, all rights reserved.
 *
 * This file is part of Gneat.
 *
 * Gneat is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License (LGPL)
 * as published by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gneat is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * and the GNU General Public License, for completeness, along with Gneat.  
 * If not, see <http://www.gnu.org/licenses/>.
 */
package org.neatshell.gneat.client.presenter.core;

import java.util.HashMap;

import org.neatshell.gneat.client.GneatCore;

public class PresenterCache {
	private static HashMap<Object, IPresenter> _$presenters;

	public static IPresenter get(Object node) {
		return get(node, true);
	}

	public static IPresenter get(Object node, boolean create) {
		if (node == null) return PresenterNull.get();
		if (_$presenters == null) _$presenters = new HashMap<Object, IPresenter>();		
		if (!_$presenters.containsKey(node)) {
			if (create) {
				put(new Presenter(GneatCore.getCoreInjector().getView().setNode(node)));
//				IPresenter presenter = new Presenter(GneatCore.getCoreInjector().getView().setNode(node));
//				_$presenters.put(node, presenter);				
//				return presenter;
			}
			else return PresenterNull.get();
		}
		return _$presenters.get(node);
	}

	public static void put(IPresenter presenter) {
		if (_$presenters == null) _$presenters = new HashMap<Object, IPresenter>();
		if (presenter.getView().getNode() != null)
			_$presenters.put(presenter.getView().getNode(), presenter);
	}

	public static PresenterNull remove(IPresenter presenter) {
		if (_$presenters != null) _$presenters.remove(presenter);
		return PresenterNull.get();
	}
	
//	public static void clear() {
//		if (_$presenters != null) _$presenters.clear();
//		_$presenters = null;
//	}
}